﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx.WoWInternals;
using Styx;
using Styx.Common;
using System.Threading;

namespace ShutUp
{
    class FriendsList
    {
        public static List<string> inGameFriends = new List<string>();
        public static List<string> BNetFriends = new List<string>();
        public static List<string> guildMembers = new List<string>();
        public static List<string> guildOffis = new List<string>();
        public static string guildMaster = string.Empty;
        public static Thread newThread;

        private static int numFriends = 0;
        private static int numGuildMembers = 0;
        private static int numBNetFrieds = 0;

        public static void Update()
        {
            if (newThread != null && newThread.IsAlive)
                return;

            newThread = new Thread(_update) { IsBackground = true, Name = "update friendlist" };
            newThread.Start();
        }

        private static void _update()
        {
            numFriends = Lua.GetReturnVal<int>("return GetNumFriends()", 0);
            numGuildMembers = Lua.GetReturnVal<int>("local numTotal, numOnline = GetNumGuildMembers(); return numTotal", 0);
            numBNetFrieds = Lua.GetReturnVal<int>("local numBNetTotal, numBNetOnline = BNGetNumFriends(); return numBNetTotal", 0);
            //Logging.Write("numBNetFrieds: {0}", numBNetFrieds);

            if (inGameFriends.Count != numFriends)
                update_FriedsList();

            if (guildMembers.Count != numGuildMembers)
                update_GuildList();

            if (BNetFriends.Count != numBNetFrieds)
                update_BNetFriends();
        }

        private static void update_FriedsList()
        {
            inGameFriends.Clear();

            for (int i = 1; numFriends != 0 && i <= numFriends; ++i)
            {
                string xName = Lua.GetReturnVal<string>("local name, level, klasse, ort, online = GetFriendInfo(" + i + "); return name", 0);
                inGameFriends.Add(xName);
            }
        }

        private static void update_GuildList()
        {
            guildMembers.Clear();
            guildOffis.Clear();
            guildMaster = string.Empty;

            for (int i = 1; numGuildMembers != 0 && i <= numGuildMembers; ++i)
            {
                //Lua.GetReturnVal<string>("local name, rank, rankIndex, level, class, zone, note, officernote, online, status, classFileName, achievementPoints, achievementRank, isMobile = GetGuildRosterInfo(" + i + "); return name", 0);
                List<string> _info = Lua.GetReturnValues("return GetGuildRosterInfo(" + i + ");");
                string _name = _info[0];
                int _rank = Convert.ToInt32(_info[2]);

                if (_rank == 0) guildMaster = _name;
                if (_rank == 1) guildOffis.Add(_name);
                guildMembers.Add(_name);
            }
        }

        private static void update_BNetFriends()
        {
            BNetFriends.Clear();
            try
            {
                for (int i = 1; numGuildMembers != 0 && i <= numBNetFrieds; ++i)
                {
                    //string _name = Lua.GetReturnVal<string>("presenceID, givenName, surname, toonName, toonID, client, isOnline, lastOnline, isAFK, isDND, broadcastText, noteText, isFriend, broadcastTime  = BNGetFriendInfo(" + i + "); return surname", 0);
                    string _name = Lua.GetReturnValues(String.Format("return BNGetFriendInfo({0})", i))[2];
                    BNetFriends.Add(_name);
                }
            }
            catch (Exception ex)
            {
                Logging.Write("update_BNetFriends /n{0}", ex);
            }

        }

        public static bool isFriend(string name)
        {
            Update();

            if (inGameFriends.Contains(name))
                return true;

            if (guildMembers.Contains(name))
                return true;

            if (BNetFriends.Contains(name))
                return true;

            if (ShutUpGlobalSettings.Instance.FriendList1.Contains(name))
                return true;

            return false;            
        }

        public static string Status(string name)
        {
            if (guildMembers.Contains(name))
            {
                if(guildMaster == name)
                    return Lua.GetReturnVal<string>("return GuildControlGetRankName(1);", 0);

                if(guildOffis.Contains(name))
                    return Lua.GetReturnVal<string>("return GuildControlGetRankName(1);", 0);

                if (inGameFriends.Contains(name) || ShutUpGlobalSettings.Instance.FriendList1.Contains(name))
                    return "Friend";

                return "Guildmember";
            }

            if (inGameFriends.Contains(name) || ShutUpGlobalSettings.Instance.FriendList1.Contains(name))
                return "Friend";

            if (BNetFriends.Contains(name))
                return "Bnet Friend";

            return "unknown";
        }

    }
}

